﻿Imports System.Net
Imports System.IO

Namespace SistemaINCA
    Public Class ReportesController
        Inherits System.Web.Mvc.Controller

        Private _extensionsFormat() As String = {"pdf", "doc", "xls"}

        Enum FormatExport
            PDF = 0
            Word = 1
            Excel = 2
        End Enum

        <Authorize(Roles:="ERA")> _
        <HttpGet()> _
        Function PerfilEstudiante(NIE As String, formato As FormatExport) As ActionResult
            Dim sNIE As String = HttpUtility.HtmlEncode(NIE)

            'Determinamos que tipo de formato se ha solicitado
            Dim sURL As String
            Dim sFormat As String
            Select Case formato
                Case FormatExport.PDF
                    sFormat = "pdf"
                Case FormatExport.Word
                    sFormat = "word"
                Case Else
                    Return RedirectToAction("Alumnos", "RegAcad")
            End Select
            sURL = String.Format("{0}/Alumnos/PerfilAlumno&NIE={1}&rs:Format={2}", ConfigurationManager.AppSettings("ReportServerURL"), sNIE, sFormat)

            'Creamos el WebRequest, para hacer la peticion y logearnos al servidor. 
            Dim wr As HttpWebRequest = HttpWebRequest.Create(sURL)
            wr.Credentials = New NetworkCredential(ConfigurationManager.AppSettings("ReportServerUser"),
                                                   Security.EncryptationSchoolClick.DesencriptarData(ConfigurationManager.AppSettings("ReportServerPassword")))
            wr.Timeout = System.Threading.Timeout.Infinite

            'Obtenemos el stream del reporte solicitado
            Dim wresp = wr.GetResponse

            'Retornamos el archivo al cliente que lo esta solicitando
            Return File(wresp.GetResponseStream, wresp.ContentType, String.Format("PerfilAlumno-{0}.{1}", sNIE, _extensionsFormat(formato)))
        End Function

    End Class
End Namespace
